<?php
	require_once 'RegexConfig.php';

	/**
	 * check input data format
	 * @author Louis
	 */
	class Regex{
		
		/**
		 * check account
		 */
		public static function isAccount($account, $min = ACCOUNT_LENGTH_MIN, $max = ACCOUNT_LENGTH_MAX) {
				
			$regex = '/^[0-9a-zA-Z\_]{'.$min.','.$max.'}$/';
				
			return preg_match($regex, $account) ? true : false;
		}
		
		/**
		 * check password
		 */
		public static function isPassword($password, $min = PASSWORD_LENGTH_MIN, $max = PASSWORD_LENGTH_MAX) {
		
			$regex = '/^[0-9a-zA-Z]{'.$min.','.$max.'}$/';
		
			return preg_match($regex, $password) ? true : false;
		}
		
		/**
		 * check email
		 */
		public static function isEmail($email, $max = EMAIL_LENGTH_MAX) {
			
			$regex = '/(\w+@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i';
			
			return preg_match($regex, $email) && strlen($email) <= $max ? true : false;
		}
		
		/**
		 * check phone
		 */
		public static function isPhone($phone, $max = PHONE_LENGTH_MAX) {
				
			$regex = '/0\d{1}-\d{8}|0\d{3}-\d{6}}|\(0\d{1}\)\d{4}-\d{4}/|0\d{9}';
			//$regex = '';
			return preg_match($regex, $phone) ? true : false;
		}
		
		/**
		 * check editor
		 */
		public static function isEditor($editor, $max = EDITOR_LENGTH_MAX) {
			
			$regex = '/<[Ss][Cc][Rr][Ii][Pp][Tt]|<\/[Hh][Tt][Mm][Ll]|<\/[Bb][Oo][Dd][Yy]|<\?|\?>|<%|%>|<!--|-->/';
		
			return preg_match($regex, $editor) && strlen($editor) <= $max ? true : false;
		}
		
		/**
		 * check url
		 */
		public static function isUrl($url, $max = URL_LENGTH_MAX) {
			return filter_var($url, FILTER_VALIDATE_URL) && strlen($url) <= $max ? true : false;
		}
		
		
		/**
		 * check number
		 */
		public static function isNumber($number, $max = NUMBER_LENGTH_MAX) {
			return filter_var($number, FILTER_VALIDATE_INT) && strlen($number) <= $max ? true : false;
		}
		
		/**
		 * check float
		 */
		public static function isFloat($float, $max = FLOAT_LENGTH_MAX) {
			return filter_var($float, FILTER_VALIDATE_FLOAT) && strlen($float) <= $max ? true : false;
		}
		
		/**
		 * 檢查標點符號
		 */
		public static function isPunctuation($text) {
				
			$regex = '/[ ,\`,\~,\!,\@,\#,\$,\%,\^,\+,\*,\&,\,,\?,\|,\:,\.,\<,\>,\{,\},\(,\),\',\;,\=,\-,\"]+/';
		
			return preg_match($regex, $text) ? true : false;
		}
		
		/**
		 * 檢查HTML TAG
		 */
		public static function isHtmlTag($text) {
		
			$regex = '/<([\/]?[a-za-z]+)[^>]*>/';
		
			return preg_match($regex, $text) ? true : false;
		}
		
		/**
		 * 檢查字串是否在範圍內
		 */
		public static function isTextRange($text, $min, $max) {
		
			$textLength = strlen($text);
		
			return $textLength >= $min && $textLength <= $max ? true : false;
		}
			
	}
	
?>